/**
 * Created by umakr on 2018/6/11.
 */
var API_TEXT = 'http://restapi.amap.com/v3/place/text?';
var API_GET_ADR = 'http://api.map.baidu.com/location/ip?ak=mgiNQ25duGhPsEQpCYUeG19MDkBSRoN9&coor=bd09ll';
var API = 'http://restapi.amap.com/v3/place/around?';
var API_AMP_BD='http://api.map.baidu.com/geoconv/v1/?output=json&from=3&ak=mgiNQ25duGhPsEQpCYUeG19MDkBSRoN9&coords=';
var MAP_IMAGE_URL='http://api.map.baidu.com/staticimage/v2?ak=mgiNQ25duGhPsEQpCYUeG19MDkBSRoN9&center='


var map_pageIndex = undefined;
var map_maxItems = undefined;
var map_lastItemIndex = undefined;
var map_lat  = undefined;
var map_lng  = undefined;
var map_root  = undefined;
function searchPageable(text,lng,lat,city,page,pageSize,root) {
  map_root  = root;
  if (text) {
    doSch(text, city, page, pageSize);
  } else {
    if(lat) map_lat = lat;
    if(lng) map_lng = lng;
    var url = 'http://restapi.amap.com/v3/assistant/coordinate/convert?coordsys=baidu&locations='+map_lng+','+map_lat+'&key=292a55cdd01603ac8414bf5acaea493d';
   // console.log("url",url);
    app.request.getJSON(url, function (ret) {
      // console.log("坐标转换",ret)
      if (ret.status === '1') {
        doSch(page, pageSize, ret);
      }
    });
  }
}

function doSch(page,pageSize,ret){
  var radius = 1000;
  if(!page) page=map_pageIndex
  var params = {
    key:'292a55cdd01603ac8414bf5acaea493d',
    types:'050000|070000|120000|150000|140000|100000|090000|060000|010000|200000|170000|160000|130000|110000',
    radius:radius,
    offset:pageSize,
    output:'JSON',
    page:page,
  };
  params.location = ret.locations;
  if(ret.status === '1'){
    // console.log("搜索参数",params);
    fetchGeoData(params);
  }
}

function fetchGeoData(params){
  app.request.getJSON(API,params,function(res){
    appendMapList(res,params.page);

  });
}

function appendMapList(res,page){
  map_pageIndex = page+1;
  // console.log("搜索结果",res);
  if(res.status === '1' && res.pois.length>0) {
    map_maxItems = res.count;
    var pois = res.pois;
    if(page == 1){
      $$('#map-list ul').html("");
      clickMapList(pois[0])
    }

    var html = '';
    for (var i = 0; i < pois.length; i++) {
      var Poi = pois[i];
      var input = '<input type="radio" name="demo-radio" />'
      if(page ===1&&i===0){
        input = '<input type="radio" name="demo-radio" checked />'
      }
      html += '<li onclick=clickMapList('+ JSON.stringify(Poi).replace(/"/g, '&quot;') +')>'
        + '<label class="item-radio item-content no-ripple">'
        + input
        +'<i class="icon icon-radio"></i>'
        +'<div class="item-inner" style="float: none;display : block;font-size: 14px">'
        +'<div  class="item-title" style="float: none;display : block;margin-bottom: 10px">'+Poi.name+'</div>'
        +'<div  class="item-title" style="float: none;display : block;color: #9d9d9d;font-size: 10px">'+Poi.pname +Poi.cityname +Poi.adname +Poi.address+'</div>'
        +'</div>'
        +'</label>'
        +'</li>';
    }
    $$('#map-list ul').append(html);
    map_lastItemIndex = $$('#map-list li').length;
    if(map_lastItemIndex == map_maxItems){
      //隐藏无限滚动
//        app.detachInfiniteScroll($$('.infinite-scroll'));
      $$('.infinite-scroll-preloader').hide();
      $$('.infinite-scroll-nomore').show();
    }
  }else{
    //隐藏无限滚动
//      app.detachInfiniteScroll($$('.infinite-scroll'));
    $$('#map-scroll-preloader').hide();
    $$('#map-scroll-nomore').show();
  }
}

function clickMapList(Poi) {
  // console.log("",Poi)
  // console.log("",Poi.name)
  window.helpPositionName = Poi.name;
  window.helpPositionFullName = Poi.pname+Poi.cityname+Poi.adname+Poi.address;
  var url = API_AMP_BD + Poi.location;
  $.ajax({
    url: url,
    type: 'get',
    dataType: 'jsonp',  // 请求方式为jsonp
    jsonpCallback: "clickMapListBack",    // 自定义回调函数名
  });
}

function clickMapListBack(re){
  map_root.emit("mapList.marker",re,false);
  // console.log("高德转百度",re.result[0].x,re.result[0].y);
  // if(re.status === 0){
  //     window.map={
  //       lng:re.result[0].x,
  //       lat:re.result[0].y,
  //     }
  //     // window.isSaveAdr = false;
  //     // makeMarker(re.result[0].x,re.result[0].y,false);
  //   map_root.emit("mapList.marker",re,false);
  //   }
}


function attribute(){
  var p = marker.getPosition();  //获取marker的位置
  alert("marker的位置是" + p.lng + "," + p.lat);
}





var textSearch_pageIndex = undefined;
var textSearch_maxItems = undefined;
var textSearch_lastItemIndex = undefined;
var textSearch_searchText = undefined;
var textmap_root = undefined
function doTextSch(text,city,page,pageSize,root){
  textmap_root =root;
  textSearch_searchText = text;
  if(!page) page=textSearch_pageIndex;
  var radius = 1000;
  var params = {
    key:'292a55cdd01603ac8414bf5acaea493d',
    types:'050000|070000|120000|150000|140000|100000|090000|060000|010000|200000|170000|160000|130000|110000',
    radius:radius,
    offset:pageSize,
    output:'JSON',
    page:page,
  };

  params.keywords = text;
  params.city=city?city:"乌鲁木齐";
  params.citylimit=true;
  fetchGeoTextData(params);
}

function fetchGeoTextData(params){
  app.request.getJSON(API_TEXT,params,function(res){
    appendTextMapList(res,params.page);
  });
}
function appendTextMapList(res,page){
// console.log("文字搜索结果",res,page);
  if(res.status === '1' && res.pois.length>0) {
    textSearch_maxItems = res.count;
    var pois = res.pois;
    if(page==1){
      $$('#text-map-list ul').html("");
    }
    textSearch_pageIndex = page+1;

    var html = '';
    for (var i = 0; i < pois.length; i++) {
      // var Poi = pois[i];
      // html += '<li style="height: auto;width: auto" onclick=clickTextMapList('+ JSON.stringify(Poi).replace(/"/g, '&quot;') +')>'
      //   +'<div style="margin-top: 5px;">'
      //   +'<div style="font-size:14px;line-height: 14px;margin-top: 10px " >'+Poi.name+'</div>'
      //   +'<div style="margin-top: 10px;margin-bottom:10px;font-size: 12px;line-height: 12px;width: 380px;overflow: hidden;text-overflow: ellipsis;">'+Poi.pname +Poi.cityname +Poi.adname +Poi.address+'</div>'
      //   +'</div>'
      //   +'</li>';
      var Poi = pois[i];
      var input = '<input type="radio" name="demo-radio" />'
      if(page ==1&&i==0){
        input = '<input type="radio" name="demo-radio" checked />'
      }
      html += '<li onclick=clickTextMapList('+ JSON.stringify(Poi).replace(/"/g, '&quot;') +')>'
        + '<label class="item-radio item-content no-ripple">'
        + input
        +'<i class="icon icon-radio"></i>'
        +'<div class="item-inner" style="float: none;display : block;font-size: 14px">'
        +'<div  class="item-title" style="float: none;display : block;margin-bottom: 10px">'+Poi.name+'</div>'
        +'<div  class="item-title" style="float: none;display : block;color: #9d9d9d;font-size: 10px">'+Poi.pname +Poi.cityname +Poi.adname +Poi.address+'</div>'
        +'</div>'
        +'</label>'
        +'</li>';
    }
    $$('#text-map-list ul').append(html);
    textSearch_lastItemIndex = $$('#text-map-list li').length;
    if(textSearch_lastItemIndex == textSearch_maxItems){
      $$('.infinite-scroll-preloader').hide();
      $$('.infinite-scroll-nomore').show();
    }
  }else{
    //隐藏无限滚动
//            app.detachInfiniteScroll($$('.infinite-scroll'));
    $$('.infinite-scroll-preloader').hide();
    $$('.infinite-scroll-nomore').show();
  }
}

function clickTextMapList(Poi) {
  // console.log("高德坐标",Poi.location);
  window.helpPositionName = Poi.name;
  window.helpPositionFullName = Poi.pname+Poi.cityname+Poi.adname+Poi.address;
  var url = API_AMP_BD + Poi.location;
  $.ajax({
    url: url,
    type: 'get',
    dataType: 'jsonp',  // 请求方式为jsonp
    jsonpCallback: "clickTextMapListBack",    // 自定义回调函数名
  });
}
function clickTextMapListBack(ret) {
  textmap_root.back("/baiduMap/");
  textmap_root.emit("mapList.marker",ret,true);

}
